//
//  HMComposePictureView.swift
//  Weibo22
//
//  Created by Apple on 16/12/9.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit


//  重用标记
private let HMComposePictureViewCellIndetifier = "HMComposePictureViewCellIndetifier"

//  发微博配图视图
class HMComposePictureView: UICollectionView {

    //  点击最后一个cell执行的闭包
    var callBack: (()->())?
    
    //  数据源
    lazy var images: [UIImage] = [UIImage]()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout = UICollectionViewFlowLayout()
        
        //  每项间距
        let itemMargin: CGFloat = 5
        //  计算每项的宽度
        let itemWidth = ((ScreenWidth - 20) - 2 * itemMargin) / 3
        //  设置大小
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        //  设置行间距和列间距
        flowLayout.minimumLineSpacing = itemMargin
        flowLayout.minimumInteritemSpacing = itemMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
        
        //  注册cell
        register(HMComposePictureViewCell.self, forCellWithReuseIdentifier: HMComposePictureViewCellIndetifier)
        //  设置数据源代理
        dataSource = self
        //  设置代理
        delegate = self
        
        //  默认隐藏
        isHidden = true
        
        
    }
    
    
    //  添加图片
    func addImage(image: UIImage) {
        //  如果超过九张图片,那么不允许再添加图片
        if images.count >= 9 {
            return
        }
        
        //  显示配图
        isHidden = false
        
        images.append(image)
        
        reloadData()
        
    
    }
    

}

extension HMComposePictureView: UICollectionViewDataSource, UICollectionViewDelegate {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if images.count == 0 || images.count == 9 {
            return images.count
        } else {
            return images.count + 1
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HMComposePictureViewCellIndetifier, for: indexPath) as! HMComposePictureViewCell
        if indexPath.item == images.count {
            cell.image = nil
        } else {
            //  绑定图片
            cell.image = images[indexPath.item]
            cell.indexPath = indexPath
            cell.deleteImageClosure = { [weak self] (index) -> Void in
                //  删除图片
                self?.images.remove(at: index)
                
                if self?.images.count == 0 {
                    //  隐藏配图
                    self?.isHidden = true
                }
                
                //  重新加载数据
                self?.reloadData()
            
            }
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.item == images.count {
            //  表示点击的是最后一个cell
            callBack?()
        }
    }
    
    
    
}

//  自定义发微博配图cell
class HMComposePictureViewCell: UICollectionViewCell  {

    //  MARK: -- 懒加载
    private lazy var imageView: UIImageView = UIImageView()
    //  设置当前cell对应的indexPath
    var indexPath: IndexPath?
    //  点击删除图片按钮执行的闭包
    var deleteImageClosure: ((Int)->())?
    
    //  删除图片按钮
    private lazy var deleteImageButton: UIButton = {
        let button = UIButton()
        //  添加点击事件
        button.addTarget(self, action: #selector(deleteImageAction), for: .touchUpInside)
        button.setImage(UIImage(named: "compose_photo_close"), for: .normal)
        return button
    }()
    
    var image: UIImage? {
        didSet {
            
            if image == nil {
                imageView.image = UIImage(named: "compose_pic_add")
                //  如果是加号图片,那么隐藏删除图片按钮
                deleteImageButton.isHidden = true
            } else {
                imageView.image = image
                //  显示删除图片按钮
                deleteImageButton.isHidden = false
            }
            
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  添加控件设置约束
    private func setupUI() {
    
        contentView.addSubview(imageView)
        contentView.addSubview(deleteImageButton)
        imageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
        deleteImageButton.snp_makeConstraints { (make) in
            make.top.equalTo(imageView)
            make.trailing.equalTo(imageView)
        }
        
        
    }
    
    //  MARK: -- 点击删除按钮的操作
    @objc private func deleteImageAction() {
    
        //
        if let currentIndexPath = indexPath {
            //  点击删除图片按钮执行的闭包
            deleteImageClosure?(currentIndexPath.item)
        }
        
       
        
        
    }
    

}







